/**************************************************************************//**
 * @item     CosyOS Kernel
 * @file     tsv.c
 * @brief    Service for SysTick_Handler(Tick-Hook, Timing Interrupt-Hook, Timing Query-Hook)
 * @author   迟凯峰
 * @version  V2.3.3
 * @date     2023.05.13
 ******************************************************************************/

#include "glovar.h"
#include "sysapi.h"
#include "svs.h"

tECODE resume_task__(tspTaskNode task_node) __C51USING__
{
	#if SYSCFG_TASKCREATEMODE == __DYNAMIC__
	if(task_node == NULL) return ECODE_TASKNOTSTARTED;
	#endif
	if(task_node->state & __SUSPENDED__)
	{
		task_node->state &= __RESUME__;
		return ECODE_NOERR;
	}
	if(task_node->state < __STOPPED__)  return ECODE_TASKUNSUSPENDED;
	if(task_node->state & __STOPPED__)  return ECODE_TASKSTOPPED;
	if(task_node->state == __DELETED__) return ECODE_TASKNOTSTARTED;
	return ECODE_DONOTKNOW;
}

tECODE suspend_task__(tspTaskNode task_node) __C51USING__
{
	#if SYSCFG_TASKCREATEMODE == __DYNAMIC__
	if(task_node == NULL) return ECODE_TASKNOTSTARTED;
	#endif
	if(task_node->state < __SUSPENDED__)
	{
		task_node->state |= __SUSPENDED__;
		return ECODE_NOERR;
	}
	if(task_node->state & __SUSPENDED__) return ECODE_TASKSUSPENDED;
	if(task_node->state & __STOPPED__)   return ECODE_TASKSTOPPED;
	if(task_node->state == __DELETED__)  return ECODE_TASKNOTSTARTED;
	return ECODE_DONOTKNOW;
}

tECODE delete_task__(tspTaskNode task_node) __C51USING__
{
	#if SYSCFG_TASKCREATEMODE == __DYNAMIC__
	if(task_node == NULL) return ECODE_TASKNOTSTARTED;
	#endif
	if(task_node->state < __DELETED__)
	{
		task_node->state = __DELETED__;
		return ECODE_NOERR;
	}
	if(task_node->state == __DELETED__) return ECODE_TASKNOTSTARTED;
	return ECODE_DONOTKNOW;
}

tECODE set_priority__(tspTaskNode task_node, u8 tpl) __C51USING__
{
	#if SYSCFG_TASKCREATEMODE == __DYNAMIC__
	if(task_node == NULL) return ECODE_TASKNOTSTARTED;
	#endif
	if(task_node->state < __STOPPED__)
	{
		if(task_node->TPL != tpl)
		{
			task_node->TPL = tpl;
			sSetPriority(task_node);
			return ECODE_NOERR;
		}
		return ECODE_TPLUNCHANGED;
	}
	if(task_node->state & __STOPPED__)  return ECODE_TASKSTOPPED;
	if(task_node->state == __DELETED__) return ECODE_TASKNOTSTARTED;
	return ECODE_DONOTKNOW;
}

#if SYSCFG_MSGQUEUE == __ENABLED__

tECODE send_msg__(tspMsgQueue msg_queue, void *msg) __C51USING__
{
	msg_queue->mutex = false;
	if(msg_queue->counter == msg_queue->len)
	{
		msg_queue->mutex = true;
		vAlarm.overflow_msgqueue = true;
		return ECODE_OVERFLOW_MSGQUEUE;
	}
	if(msg_queue->type == __DYNAMIC__)
	{
		tspMsgNode msg_node = NULL;
		msg_node = (tspMsgNode)__malloc(sizeof(tsMsgNode));
		if(msg_node == NULL)
		{
			msg_queue->mutex = true;
			vFault.mallocfail_msgnode = true;
			return ECODE_MALLOCFAIL_MSGNODE;
		}
		sSendMsg_Dynamic(msg);
	}
	else
	{
		sSendMsg_Static(msg);
	}
	msg_queue->mutex = true;
	return ECODE_NOERR;
}

void *recv_msg__(tspMsgQueue msg_queue) __C51USING__
{
	void *msg = NULL;
	tspMsgNode msg_node;
	msg_queue->mutex = false;
	if(!msg_queue->counter);
	else if(msg_queue->type == __DYNAMIC__)
	{
		sRecvMsg_Dynamic(msg);
	}
	else
	{
		sRecvMsg_Static(msg);
	}
	msg_queue->mutex = true;
	return msg;
}

#endif
